package zaizai.com.slice;

import com.ajguan.ResourceTable;
import com.ajguan.library.EasyRefreshLayout;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.text.Font;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import zaizai.com.SimpleAdapter;
import zaizai.com.Toast;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 刷新加载界面
 */
public class RefreshLoadingAbilitySlice extends AbilitySlice {
    private static final String TAG = RefreshLoadingAbilitySlice.class.getSimpleName();
    private EasyRefreshLayout easyRefreshLayout;
    private ListContainer listContainer;
    private SimpleAdapter adapter;
    private EventHandler eventHandler;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent((ComponentContainer) LayoutScatter
                .getInstance(RefreshLoadingAbilitySlice.this)
                .parse(ResourceTable.Layout_ability_only_loading, null, false));
        initView();
        initListener();
        initData();
    }


    @Override
    protected void onStop() {
        easyRefreshLayout.release();
        easyRefreshLayout = null;
        eventHandler.removeAllEvent();
        eventHandler = null;
        super.onStop();
    }

    /**
     * 模拟数据
     */
    private void initData() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("EasyRefreshLayout index :" + i);
        }
        adapter.getData().addAll(list);
        adapter.notifyDataChanged();

    }

    /**
     * 设置监听
     */
    private void initListener() {
        easyRefreshLayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            @Override
            public void onLoadMore() {

                final List<String> list = new ArrayList<>();
                for (int i = 0; i < 5; i++) {
                    list.add("this is  new load data >>>>" + new Date().toLocaleString());
                }
                if (eventHandler != null) {
                    eventHandler.postTask(() -> {
                        easyRefreshLayout.closeLoadView();
                        adapter.getData().addAll(list);
                        adapter.notifyDataChanged();
                        listContainer.scrollBy(0, AttrHelper.vp2px(60, RefreshLoadingAbilitySlice.this));//偏移一个item高度

                    }, 500);
                }
            }

            @Override
            public void onRefreshing() {
                if (eventHandler != null) {
                    eventHandler.postTask(() -> {
                        List<String> list = new ArrayList<>();
                        for (int i = 0; i < 20; i++) {
                            list.add("this is refresh data >>>" + new Date().toLocaleString());
                        }
                        adapter.setNewData(list);
                        easyRefreshLayout.refreshComplete();
                        Toast.showToast(RefreshLoadingAbilitySlice.this, "refresh success");
                    }, 1000);
                }
            }
        });
    }

    /**
     * 初始化view
     */
    private void initView() {
        eventHandler = new EventHandler(EventRunner.getMainEventRunner());
        Button titleBar = (Button) findComponentById(ResourceTable.Id_title_bar);
        titleBar.setFont(Font.DEFAULT_BOLD);
        easyRefreshLayout = (EasyRefreshLayout) findComponentById(ResourceTable.Id_easylayout);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        adapter = new SimpleAdapter();
        listContainer.setItemProvider(adapter);
    }
}
